package org.shean.app.rssreader.utils;

import org.shean.app.rssreader.provider.SzRSSReader;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;

public class RssUtils {
	public static int[] sViewList = new int[0];
	public static int sCurrentPoistion;

	public static void viewCurrentPost(Context context, Cursor cursor, int position,
			long id, boolean isTag) {
		
		sCurrentPoistion = position;
		if (cursor == null) {
			sViewList = new int[0];
			return;
		}
		int len = cursor.getCount();
		sViewList = new int[len];
		cursor.moveToFirst();
		
		int colidx = cursor.getColumnIndexOrThrow(SzRSSReader.Posts._ID);
		
		if(isTag)
		{
			colidx = cursor.getColumnIndexOrThrow(SzRSSReader.PostTags.POST_ID);
		}
		for (int i = 0; i < len; i++) {
			sViewList[i] = cursor.getInt(colidx);
			cursor.moveToNext();
		}

		Uri uri = ContentUris.withAppendedId(SzRSSReader.Posts.CONTENT_URI,  sViewList[sCurrentPoistion]);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		context.startActivity(intent);
	}

	public static void viewNextPost(Context context)
	{
		if(sCurrentPoistion == sViewList.length -1)
			return;
		
		sCurrentPoistion ++;
		Uri uri = ContentUris.withAppendedId(SzRSSReader.Posts.CONTENT_URI, sViewList[sCurrentPoistion]);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		context.startActivity(intent);
	}
	
	public static void viewPrevPost(Context context)
	{
		if(sCurrentPoistion == 0)
			return;
		
		sCurrentPoistion --;
		Uri uri = ContentUris.withAppendedId(SzRSSReader.Posts.CONTENT_URI, sViewList[sCurrentPoistion]);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		context.startActivity(intent);
		
	}
	
}
